class SongritController < ApplicationController
#  after_filter :post_log
  include ActionView::Helpers::DebugHelper
  include ERB::Util

  def mark_cert101015
    User.all(:conditions=>"graduation_on > '2010-09-15'").each do |u|
      u.update_attribute :graduation_on, '2010-10-15'
    end
    render :text => "done"
  end
  def mark_alluser53
    User.certified53.each do |u|
      u.update_attribute :graduation_on, '2010-9-15'
    end
    render :text => "done"
  end
  def test_tr
    a='123'
    render :text => thai_num(a)
  end
  def cert53
    debug User.certified.map(&:id)
    # [78, 121, 129, 169, 177, 214, 219, 300, 321, 342, 344, 346, 347, 348, 352, 368, 384, 386, 395, 400, 401, 417, 423, 424, 428, 445, 446, 449, 451, 454, 456, 474, 479, 502, 503, 524, 540, 541, 542, 545, 546, 547, 550, 553, 566, 582, 585, 593, 600, 601, 602, 603, 615, 621, 627, 631, 632, 654, 663, 679, 681, 693, 708, 711, 718, 721, 723, 724, 725, 729, 731, 749, 755, 764, 765, 770, 782, 796, 799, 805, 806, 807, 815, 821, 855, 858, 865, 869, 871, 879, 885, 887, 893, 909, 928, 930, 932, 933, 938, 944, 950, 955, 958, 963, 969, 984, 995, 1011, 1016, 1019, 1021, 1028, 1038, 1050, 1060, 1062, 1063, 1064, 1065, 1067, 1068, 1069, 1078, 1084, 1085, 1088, 1089, 1105, 1109, 1111, 1235, 1253, 1263, 1264, 1265, 1295, 1303, 1324, 1327, 1330, 1347, 1373, 1374, 1375, 1376, 1378, 1391, 1397, 1398, 1403, 1409, 1410, 1411, 1412, 1413, 1434, 1439, 1441, 1442, 1456, 1457, 1464, 1466, 1477, 1482, 1484, 1485, 1486, 1488, 1490, 1491, 1493, 1495, 1496, 1498, 1500, 1501, 1502, 1505, 1507, 1509, 1514, 1515, 1517, 1518, 1537, 1558, 1560, 1566, 1568, 1569, 1583, 1584, 1590, 1630, 1645, 1656, 1662, 1691, 1694, 1695, 1705, 1714, 1722, 1723, 1724, 1765, 1770, 1793, 1795, 1806, 1812, 1837, 1838, 1850, 1854, 1856, 1873, 1874, 1876, 1878, 1880, 1882, 1883, 1892, 1894, 1897, 1918, 1927, 1930, 1932, 1933, 1934, 1936, 1937, 1939, 1942, 1943, 1944, 1945, 1946, 1947, 1950, 1952, 1954, 1955, 1957, 1958, 1960, 1964, 1966, 1967, 1969, 1970, 1973, 1976, 1980, 1982, 1986, 1988, 1993, 1994, 1995, 1997, 1998, 2008, 2013, 2016, 2021, 2025, 2027, 2032, 2035, 2036, 2037, 2041, 2042, 2046, 2047, 2048, 2055, 2097, 2099, 2103, 2104, 2105, 2109, 2113, 2119, 2126, 2127, 2129, 2131, 2132, 2133, 2134, 2136, 2142, 2158, 2163, 2164, 2167, 2202, 2204, 2211, 2212, 2221, 2224, 2227, 2228, 2229, 2232, 2233, 2234, 2235, 2236, 2237, 2238, 2242, 2246, 2248, 2249, 2269, 2284, 2285, 2288, 2290, 2294, 2295, 2296, 2297, 2299, 2300, 2305, 2307, 2310, 2311, 2315, 2316, 2317, 2318, 2323, 2328, 2331, 2332, 2333, 2335, 2336, 2337, 2338, 2339, 2341, 2342, 2344, 2346, 2352, 2355, 2358, 2360, 2366, 2374, 2375, 2383, 2385, 2387, 2397, 2399, 2419, 2421, 2423, 2429, 2430, 2432, 2436, 2443, 2444, 2445, 2447, 2452, 2468, 2469, 2471, 2472, 2475, 2477, 2478, 2479, 2480, 2481, 2483, 2484, 2487, 2488, 2489, 2490, 2491, 2493, 2496, 2497, 2498, 2500, 2501, 2514, 2521, 2535, 2538, 2540, 2544, 2550, 2551, 2555, 2556, 2558, 2561, 2563, 2565, 2569, 2576, 2581, 2582, 2583, 2584, 2591, 2592, 2593, 2594, 2597, 2605, 2608, 2609, 2611, 2614, 2617, 2619, 2622, 2623, 2624, 2625, 2626, 2627, 2629, 2637, 2642, 2643, 2647, 2659, 2664, 2667, 2668, 2669, 2670, 2671, 2673, 2675, 2678, 2679, 2682, 2683, 2684, 2685, 2686, 2690, 2691, 2699, 2704, 2707, 2719, 2720, 2730, 2731, 2732, 2733, 2735, 2737, 2741, 2746, 2747, 2748, 2749, 2750, 2752, 2763, 2764, 2770, 2772, 2776, 2777, 2778, 2784, 2785, 2788, 2791, 2803, 2807, 2808, 2809, 2812, 2815, 2819, 2820, 2822, 2823, 2837, 2838, 2839, 2842, 2844, 2845, 2846, 2847, 2848, 2850, 2853, 2856, 2860, 2862, 2867, 2870, 2873, 2874, 2879, 2887, 2888, 2890, 2891, 2893, 2895, 2896, 2903, 2904, 2906, 2908, 2910, 2912, 2913, 2914, 2916, 2917, 2918, 2926, 2928, 2932, 2933, 2935, 2936, 2938, 2940, 2941, 2943, 2947, 2949, 2950, 2951, 2952, 2953, 2954, 2955, 2956, 2958, 2964, 2966, 2967, 2968, 2974, 2977, 2979, 2980, 2987, 2989, 2995, 2998, 3000, 3004, 3005, 3007, 3015, 3016, 3017, 3018, 3019, 3020, 3021, 3022, 3023, 3024, 3025, 3027, 3028, 3029, 3030, 3031, 3032, 3033, 3034, 3037, 3038, 3040, 3041, 3042, 3044, 3045, 3047, 3048, 3049, 3050, 3051, 3052, 3054, 3056, 3059, 3060, 3068, 3069, 3070, 3077, 3079, 3085, 3088, 3095, 3116, 3122, 3133, 3134, 3136, 3139, 3140, 3141, 3143, 3146, 3148, 3149, 3150, 3151, 3155, 3158, 3161, 3162, 3163, 3164, 3166, 3167, 3169, 3172, 3173, 3174, 3175, 3177, 3181, 3182, 3185, 3186, 3187, 3195, 3199, 3220, 3232, 3234, 3235]
  end
  def test_date15
    render :text => Date.today.day
  end
  def fix_posts
    Post.all.each do |p|
      if p.user && p.user.blog
        p.update_attribute :blog_id, p.user.blog.id
      else
        p.destroy
      end
    end
    render :text => "done"
  end
  def test_log
    year=2009
    dstart= Time.local year,1,1
    dend= Time.local year,12,31,23,59,59
    l= Log.all( :conditions=>["action='video_stream' AND created_at>=? AND created_at<=?", dstart, dend])
    render :text => debug(l)
  end
  def test_chat
    current_time= Time.now.utc
    chat_session= ChatSession.first :conditions=>["start <= ? AND stop >= ?",current_time, current_time]
    render :text => debug(chat_session)
  end
  def test_flash
    flash[:notice]="aa"
    redirect_to :controller=>"main"
  end
  def test_url
    render :text => "http://#{request.host_with_port}/public/aa.txt"
  end
  def test_cp
    FileUtils.cp "C:\\mict_tgel\\sqlite\\sqlite3.dll", "public"
    render :text => Rails.root+"/public"
  end
  def test_passed
    e= Exam.find 4
    render :text => e.passed?(60)
  end
  def test_qualified
    e= Exam.find 5
    render :text => e.qualified?(current_user)
  end
  def test_op_email
    users = User.find :all,
      :select=> "DISTINCT email",
      :conditions => ["role like ? or role like ? or role like ?","%A%", "%T%", "%D%"]
    email_list = []
    users.each do |user|
      email_list.push(user.email)
    end
    render :text => email_list.join(",")
  end
  def test_date
    u= User.find 73
    d= u.dob
    be_date= Date.new d.year-543, d.month, d.day
    render :text => be_date
  end
  def gen_user
    users= [{:login=>'arnat', :fname=>'อาณัติ', :lname=>'ลีมัคเดช'},
      {:login=>'nas', :fname=>'เนรศ', :lname=>'คำเชื้อ'},
      {:login=>'bua', :fname=>'ชลลดา', :lname=>'อมรฉัตร'},
      {:login=>'vanida', :fname=>'วนิดา', :lname=>'ประทีปเสน'},
      {:login=>'chaiyo', :fname=>'ชัยโย', :lname=>'เตโชนิมิตร'},
      {:login=>'orawan', :fname=>'อรวรรณ', :lname=>'ลีมัคเดช'}
    ]
    u = User.find 1
    users.each do |uu|
      old_user = User.find_by_login uu[:login]
      old_user.destroy if old_user
      new_user= User.new :password=>'1234', :nid=>'1234567890123', :email=>'songrit@thai.com'
      new_user.login = uu[:login]
      new_user.fname = uu[:fname]
      new_user.lname = uu[:lname]
      new_user.role = "A,T,D"
      new_user.save
    end
    render :text => "done"
  end
  def test_jug
#    Juggernaut.send_to_all("alert('hi from juggernaut')")
    text= "ภาษาไทย sldklskdls lskds sl dlsa dlskalsdakdlsakds sdkslkd ls alkaslkdl sss"
    Juggernaut.send_to_all("$('main').appendText('#{text}')")
    render :text => "done"
  end
  def fix_session_id
    Log.find(:all).each do |log|
      log.session_id = log.session.session_id
    end
    render :text => "done"
  end
  def test
    render :text => "hello"
  end
  def populate_material
    Material.find(:all, :conditions=>['course_id=?',2]).each do |material|
      mat2= Material.new :name=> material.name,
        :description=>material.description,
        :filename=>material.filename,
        :size=>material.size,
        :course_id=> 5
      mat2.save
    end
    render :text => "done"
  end
  def test_proxy
    t = http("http://www.google.com")
    render :text=>t
  end
  def test_mail
    cmd= []
    cmd << "echo bbb>> /usr/rails_apps/elearn/log/system.log ;"
    cmd << "http_proxy=http://172.16.0.5:8080 && "
    cmd << "HTTP_PROXY=http://172.16.0.5:8080 && "
    cmd << "/usr/local/bin/wget -proxy=on http://email.velocall.com/main/send_mail?to=songrit@gmail.com >>/usr/rails_apps/elearn/log/system.log ;"
    cmd << "echo $HTTP_PROXY >> /usr/rails_apps/elearn/log/system.log ;"
    t= system(cmd.join)
    render :text=>cmd.join
  end
  def test_mail2
    Mail.create :recipient=>"songrit@gmail.com", :subject=>"hello"
    render :text=>"done"
  end

	def http_agent
	  unless @http_agent
	    proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
	    if proxy
	      proxy = URI.parse(ENV['HTTP_PROXY'] || ENV['http_proxy'])
	      @http_agent = Net::HTTP::Proxy(proxy.host,proxy.port)
	    else
	      @http_agent = Net::HTTP
	    end
	  end
	  @http_agent
	end

	def testproxy2
		require "net/http"

		@word = "blank"

		proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
		proxy_host = proxy.host
		proxy_port = proxy.port

		url = URI::parse(params[:url])
		req = Net::HTTP::Get.new(url.path+'?'+url.query)
		res = nil
		res = Net::HTTP::Proxy(proxy_host,proxy_port).start(url.host, url.port) do |http|
			http.request(req)
		end
		@word = res.body
	end

end
